/*
 * Copyright 2017-2101 Innel.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hykj.xxltest.service;

import com.hykj.xxltest.base.BaseService;
import com.hykj.xxltest.mapper.UserMapper;
import com.hykj.xxltest.model.UserModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * 用户
 *
 * @author innel
 * @email 2638086622@qq.com
 * @date 2018-03-26 22:08:14
 */
@Service
public class UserService extends BaseService<UserModel, UserMapper> {

    @Autowired
    private static UserService userService;

    public static String getUserName(Integer userId){
        if(userService==null){
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getServletContext());
            userService= (UserService) ac1.getBean("userService");
            System.out.println("初始化 getUserName");
        }
        UserModel userModel=userService.getById(userId);
        if(userModel==null){
            return "";
        }
        return  userModel.getNickname();


    }

}